package tester.parser;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.ParserAdapter;
import org.xml.sax.helpers.XMLReaderAdapter;

import tester.test.Test;

/**
 * This class will parse the xml file containing the test cases
 * @author Caleb Chan
 *
 */
public class TesterXMLParser {

	private String filename;
	private ParserAdapter adapter;
	
	private ArrayList<Test> testSuite;
	
	/**
	 * TesterXMLParser constructor
	 * @param filename The file name of the file to be parsed
	 */
	public TesterXMLParser(String filename){
		this.filename = filename;
		this.testSuite = new ArrayList<Test>();
	}
	
	/**
	 * This method will parse the file
	 */
	public void parseFile(){
		if(filename == null){
			return;
		}
		// Creates a new handler
		TestContentHandler handler = new TestContentHandler();
		try{
			adapter = new ParserAdapter(new XMLReaderAdapter());
			adapter.setContentHandler(handler);
			// Parse the file
			adapter.parse((new File(filename)).getPath());
			testSuite.addAll(handler.getTestSuite());
			System.out.println("\n");
			for(Test t : testSuite){
				System.out.println("------------------------------------- Print Test ----------------------------------");
				System.out.println(t.toString());
				System.out.println("------------------------------------- END Print Test ----------------------------------");
			}
		}catch (SAXException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Returns the list of test
	 * @return Return the list of test
	 */
	public ArrayList<Test> getTestSuite() {
		return testSuite;
	}

}
